<?php namespace App\Http\Controllers\Api;

use App\Banner;
use App\Classify;
use App\HomeImg;
use App\Http\Controllers\Controller;
use App\Http\Services\FileManager;
use App\Http\Services\UserManager;
use App\Product;
use App\ProductImgs;
use App\Userinfo;
use Faker\Provider\File;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function getHomeImg(){

        $thatHomeImg = HomeImg::whereId(1)->first();
        if($thatHomeImg){
            $img_url = FileManager::getAliOssFile($thatHomeImg->img_id);
        }else{
            $img_url = "https://file.zliangchina.com/weishang/images/back.jpg";
        }

        return response()->json(["level"=>"success","message"=>"获取成功","data"=>$img_url]);
    }

    public function getBannerList(){
        $allBanner = Banner::orderBy("sort","asc")->get();

        if(count($allBanner) > 0){
            foreach ($allBanner as $banner){
                $banner->img_url = FileManager::getAliOssFile($banner->img_id);
            }
        }

        return response()->json(["level"=>"success","message"=>"获取成功","data"=>$allBanner]);
    }

    public function getClassifyList(){
        $partOneClassify = Classify::whereType("part_one")->orderBy("sort","asc")->get();
        $partTwoClassify = Classify::whereType("part_two")->orderBy("sort","asc")->get();
        $partThreeClassify = Classify::whereType("part_three")->orderBy("sort","asc")->get();

        $result["part_one_list"] = $partOneClassify;
        $result["part_two_list"] = $partTwoClassify;
        $result["part_three_list"] = $partThreeClassify;

        return response()->json(["level"=>"success","message"=>"获取成功","data"=>$result]);
    }

    public function getProductList(Request $request){
        $user = UserManager::isLogin($request);
        $classifyOne   = $request->get("classify_one");
        $classifyTwo   = $request->get("classify_two");
        $classifyThree = $request->get("classify_three");
        $limit = !is_null($request->get("limit")) ? $request->get("limit") : 8;
        $page  = !is_null($request->get("page")) ? $request->get("page") : 8;

        if(!$user)
            return response()->json(["level"=>"error","message"=>"未登录，不允许访问","data"=>"no_login"]);

        if($user->status == 0)
            return response()->json(["level"=>"error","message"=>"获取成功","data"=>"no_permission"]);

        /** @var Userinfo $userInfo */
        $userInfo = Userinfo::whereUserId($user->id)->first();

        $allProduct = Product::whereStatus(1);

        if(!is_null($classifyOne) && $classifyOne != 0 && $classifyOne != ''){
            $allProduct = $allProduct->whereClassifyOne($classifyOne);
        }

        if(!is_null($classifyTwo) && $classifyTwo != 0 && $classifyTwo != ''){
            $allProduct = $allProduct->whereClassifyTwo($classifyTwo);
        }

        if(!is_null($classifyThree) && $classifyThree != 0 && $classifyThree != ''){
            $allProduct = $allProduct->whereClassifyThree($classifyThree);
        }

        $allProduct = $allProduct->orderBy("created_at","desc")->offset($page * $limit)->limit($limit)->get();

        if(count($allProduct) > 0){
            foreach ($allProduct as $product){
                /** @var Product $product */
                if($product->video_id != 0){
                    $product->video_url = FileManager::getVideoFromOssUrl($product->video_id);
                    $product->img_list = null;
                }else{
                    $productImgs = ProductImgs::wherePId($product->id)->get();

                    $resultImgUrls = array();
                    if(count($productImgs) > 0){
                        foreach ($productImgs as $productImg){
                            $imgUrl = FileManager::getImgFromOssUrl($productImg->img_id);
                            array_push($resultImgUrls,$imgUrl);
                        }
                    }

                    $product->img_list = $resultImgUrls;
                    $product->video_url = null;
                }

                $classifyOneInfo = Classify::whereId($product->classify_one)->first();
                if($classifyOneInfo){
                    $product->tag_one = $classifyOneInfo->name;
                }else{
                    $product->tag_one = null;
                }

                $classifyTwoInfo = Classify::whereId($product->classify_two)->first();
                if($classifyTwoInfo){
                    $product->tag_two = $classifyTwoInfo->name;
                }else{
                    $product->tag_two = null;
                }

                $classifyThreeInfo = Classify::whereId($product->classify_three)->first();
                if($classifyThreeInfo){
                    $product->tag_three = $classifyThreeInfo->name;
                }else{
                    $product->tag_three = null;
                }
            }
        }

        $result["user_info"] = $userInfo;
        $result["product_list"] = $allProduct;

        return response()->json(["level"=>"success","message"=>"获取成功","data"=>$result]);
    }
}